// pages/recruit/personal/myjob/myjob.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageindex: 1,
    joblist: [],
    dialogShow: false,
    buttons: [{ text: '取消' }, { text: '确定' }],
    dialogtext:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    if (options.pageindex != ""){
      that.setData({
        pageindex: options.pageindex
      })
    }
    getApp().globalData.get("/api/user/mine_job", {}).then(res => {
      let box = res.data;
      for(let i in box){
        box[i].createdTime = box[i].createdTime.split(" ")[0];
        switch (box[i].unit){
          case "DAY":
            box[i].unit = "天";
            break
          case "HOUR":
            box[i].unit = "小时";
            break
          case "MONTH":
            box[i].unit = "月";
            break
          case "COUNT":
            box[i].unit = "次";
            break
          case "ITEM":
            box[i].unit = "单";
            break
        }
      }
      that.setData({
        joblist: box
      })
    })
  },
  onShow: function () {
    const that = this;
  },
  changeindex:function(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      pageindex: index
    })
  },
  myjobdetails: function (e) {//若是从新获取数据这里需要修改。换成传递job的指定参数
    const that = this;
    let jobid = e.currentTarget.dataset.jobid;
    console.log(jobid)
    wx.navigateTo({
      url: './myjobdetails/myjobdetails?jobid=' + jobid,
    })
  },
  cancleenter: function(e){
    const that = this;
    let dialogtext = "是否取消报名该兼职";
    that.setData({
      dialogShow: true,
      dialogtext: dialogtext,
      btntype:"enter",
      deleteid: e.currentTarget.dataset.jobid,
      deleteindex: e.currentTarget.dataset.itemindex,
    })
  },
  tapDialogButton: function(e){
    const that = this;
    let type = e.detail.index;
    if (type == 0){
      that.setData({
        dialogShow: false,
      })
    }else {
      that.deletetype()
    }
  },
  deletetype: function(){//确定按钮的后续操作
    const that = this;
    let type = that.data.btntype;
    let deleteid = that.data.deleteid;
    if (type == "enter"){//取消报名
      getApp().globalData.get("/api/user/cancel/job", {
        partTimeId: deleteid
      }).then(res => {
        console.log(res)
        if (res.code == 200) {
          wx.showToast({
            title: '取消报名成功',
            icon: "none"
          })
          let deleteindex = that.data.deleteindex;
          that.data.joblist.splice(deleteindex, deleteindex+1)
          that.setData({//操作成功后再删除
            dialogShow: false,
            joblist: that.data.joblist
          })
        }
      })
    }
  },
  callcom:function(e){//联系企业
    let tel = ""+ e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: tel
    })
  }
})